﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace DFGK_WMS.Repository
{
    public class TStationRepository : BaseRepository<StationDto>, ITStationRepository
    {
        public TStationRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }
                
        /// <summary>
        /// 获取库房(仓库)信息
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<TWarehouse> GetTwarehouseList()
        {
            try
            {
                return Context.TWarehouses.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
      
        /// <summary>
        /// 根据物料名称查询物料信息
        /// </summary>
        /// <param name="wname"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<TMaterial> GetTmaterialList(string wname, int pageIndex, int pageSize,out int totalCount)
        {
            try
            {
                var list = Context.TMaterials.AsQueryable();
                if (!string.IsNullOrEmpty(wname))
                {
                    list = list.Where(c => c.Name.Contains(wname));
                }
                totalCount = list.Count();
                list = list.OrderBy(x => x.CreateDate).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                return list.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取物料信息
        /// </summary>
        /// <returns></returns>
        public List<TMaterial> GetTmaterList()
        {
            try
            {
                return Context.TMaterials.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 添加站点数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool AddTmater(MaterDto obj)
        {
            try
            {
                TStationDatum sta = new TStationDatum();
                sta = Mapper.Map<TStationDatum>(obj);
                obj.CreateDate = DateTime.Now;
                Context.TStationData.Add(sta);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 货品明细
        /// </summary>
        /// <param name="zcode"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<TStationDatum> GetMaterList(string zcode, int pageIndex, int pageSize, out int totalCount)
        {
            try
            {
                var list = Context.TStationData.AsQueryable();
                if (!string.IsNullOrEmpty(zcode))
                {
                    list = list.Where(c => c.StationCode.Contains(zcode));
                }
                totalCount = list.Count();
                list = list.OrderBy(x => x.CreateDate).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                return list.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 导出货品明细Excel
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public List<MaterDto> GetExcel()
        {
            try
            {
                var query = Context.TStationData.AsQueryable();
                var table = from s in query
                            select new MaterDto
                            {
                                StationId = s.StationId,
                                StationCode = s.StationCode,
                                Id = s.Id,
                                PackageBarcode = s.PackageBarcode,
                                Barcode = s.Barcode,
                                MaterialId = s.MaterialId,
                                MaterialKeyId = s.MaterialKeyId,
                                BatchNo = s.BatchNo,
                                IsStocktake = s.IsStocktake,
                                CreateDate = s.CreateDate
                            };
                return table.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
